<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'basic';
$return=array();
if($_RQ['op']=='basic'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->plugin_setting('stopsetting',$postdata);
		$yuesetting=$this->plugin_setting('yuesetting');
		$yuesetting['nopei_days']=$postdata['nopei_days'];
		$this->plugin_setting('yuesetting',$yuesetting);
		if($postdata['isopen']){
			exi('提交成功，开始更新配送数据！','','jy_peimilk/stopsetting/updata/');
		}
		exi('设置成功');
	}
	$return['item']=$this->plugin_setting('stopsetting');
	$yuesetting=$this->plugin_setting('yuesetting');
	$return['item']['nopei_days']=$yuesetting['nopei_days'];
	$htmls=array(
		'type'=>'edit',
		'desc'=>'停送设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'停送设置',
				'list'=>array(
						array('type'=>'switch','title'=>'是否立即生效？','name'=>'isopen','isradio'=>'1','help'=>'可以指定那天或者那几天停送，这个停送代表所有客户的配送订单停止配送，同步到配送员也是一样的停止配送。凡是商户在后台统一停止配送的订单，一律按照客户配送规律往后自动排单'),
						array('type'=>'time_days','title'=>'设置停送日期','name'=>'nopei_days','help'=>'放假了，或者节假日放假，所以需要指定某天全部停送！同时这些天也无法设置预约配送！'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='updata'){
	$merchid=0;
	$yuesetting=$this->plugin_setting('stopsetting');
	if(!$yuesetting['isopen']){
		exi('刷新成功！','','jy_peimilk/stopsetting/');
	}
	$buymids=cache_read('buymids','jy_peimilk');
	if(!$buymids){
		$buymids=pdo_getall('jy_weishop_order',array('sendtype'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'status'=>array(1,2),'deleted'=>0,'userdeleted'=>0),array('mid'),'mid');
		paixu($buymids,'mid');
		if($buymids){
			$buymids=array_values($buymids);
			cache_write('buymids',$buymids,'jy_peimilk');
		}
	}
	$totalnum=count($buymids);
	if($buymids && $yuesetting['nopei_days']){
		$nowkey=$_RQ['nowkey']?:0;
		foreach($buymids as $k=>$item){
			if($k>=$nowkey && $k<$nowkey+10){
				$this->model('order')->upallgoodssendlog($item['mid'],$merchid);
			}
		}
	}
	if ($totalnum <= $nowkey + 10){
		cache_delete('buymids','jy_peimilk');
		exi('全部配置成功！','','jy_peimilk/stopsetting/');
	}else{
		$nowkey +=10;
		exi('开始配置'.$nowkey.'+客户！！','','jy_peimilk/stopsetting/updata/nowkey='.$nowkey);
	}
}